# coding=utf-8
from selenium import webdriver
import time

# 创建driver对象
driver = webdriver.Chrome(r'/home/python/Desktop/chromedriver')
# 需要请求的网址
driver.get('https://www.zhihu.com/signup?next=%2F')
time.sleep(5)
# xpath选择器用法选中登录按钮，点击跳转到登录页面
span = driver.find_element_by_xpath('//*[@id="root"]/div/main/div/div/div/div[2]/div[2]/span').click()

# 通过name定位元素:username
username = driver.find_element_by_name('username').send_keys('17601009336')
time.sleep(3)

# 通过name定位元素:password
password = driver.find_element_by_name('password').send_keys('123456liu')
time.sleep(5)
# 通过xpath定位登录按钮，点击登录
driver.find_element_by_xpath('//button[@type="submit"]').click()
# driver.find_element_by_xpath('//button[@type="submit"]').click()
time.sleep(3)
driver.get('https://www.zhihu.com/')
time.sleep(10)
for i in range(3):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight); var lenOfPage=document.body.scrollHeight; return lenOfPage;")
    time.sleep(5)
# 打印网页html
print(driver.page_source)
time.sleep(10)
# 关闭浏览器
# driver.quit()